package com.unswift.utils;

import java.util.Base64;

import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.unswift.annotation.api.Api;
import com.unswift.annotation.api.ApiField;
import com.unswift.annotation.api.ApiMethod;

@Api(value="数据加密公告操作类", author = "unswift", date = "2023-08-26", version = "1.0.0")
public final class EncryptionUtils {

	@ApiField("日志对象")
	protected final static Logger logger=LoggerFactory.getLogger(EncryptionUtils.class);
	
	@ApiMethod(value="获取md5签名数据", params = @ApiField("源数据"), returns = @ApiField("签名后的数据"))
	public static String md5Hex(String source) {
		return DigestUtils.md5Hex(source);
	}
	
	@ApiMethod(value="验证md5签名数据", params = {@ApiField("源数据"), @ApiField("比较的签名数据")}, returns = @ApiField("是否相等，true：相等，false：不相等"))
	public static boolean md5Validate(String source, String md5Data) {
		String sourceMd5 = DigestUtils.md5Hex(source);
		logger.error("source md5："+sourceMd5+"，target md5："+md5Data);
		return sourceMd5.equals(md5Data);
	}
	
	@ApiMethod(value="获取base64签名数据", params = @ApiField("源数据"), returns = @ApiField("签名后的数据"))
	public static String base64Encode(String source) {
		return base64Encode(source.getBytes());
	}
	
	@ApiMethod(value="获取base64签名数据", params = @ApiField("源数据"), returns = @ApiField("签名后的数据"))
	public static String base64Encode(byte[] source) {
		Base64.Encoder encoder = Base64.getEncoder();
		return encoder.encodeToString(source);
	}
	
	@ApiMethod(value="解析base64签名数据", params = @ApiField("签名数据"), returns = @ApiField("源数据"))
	public static String base64Decode(String encodeData) {
		Base64.Decoder decoder = Base64.getDecoder();
		return new String(decoder.decode(encodeData));
	}
}
